cd D:\000_UIH_Work\520_FUDAN_PHD\450_US_System\201_GEN_Script;
clc;
clear;

USR_FrameNumber = 1;
USR_LineNumber  = 6;
USR_ChannelNum  = 64;
USR_SampleNum   = 4000;

USR_ByteNumPerSample = 2;

NumberOfBytes    = USR_FrameNumber * USR_LineNumber * USR_ChannelNum * USR_SampleNum * USR_ByteNumPerSample;
NumberOfBytes    = NumberOfBytes + USR_LineNumber * USR_FrameNumber * 8;      %% Header Size
NumberOf64BitDW  = NumberOfBytes / 8;

FileACQ = fopen('Rawdata_FPGA1_Frame39.bin');
Line1_Header = fread(FileACQ, [1 8], 'uint8');


[Line1_Data ByteCnt] = fread(FileACQ, [USR_ChannelNum * USR_ByteNumPerSample USR_SampleNum ], 'uint8');
Line1_Data = Line1_Data';

Line1_Data_HEX = dec2hex(Line1_Data);

for (i = 1 : USR_ChannelNum)
    Line1_ChData(:, i) = Line1_Data(:, 2*i-1) * 256 + Line1_Data(:, 2*i);  
end

for (i = 1 : 16)
    for (k = 1 : 4)
        Line1_Exchange_ChData(:, (i-1) * 4 + k) = Line1_ChData(:, (i-1) * 4 + (4+1-k));
    end
end

for (i = 1 :  USR_SampleNum)
    for (k = 1 : USR_ChannelNum)   
        if (Line1_Exchange_ChData(i,k) > 2^15)
            Line1_Signed_ChData(i,k) = (-1)* (2^16 - Line1_Exchange_ChData(i,k));
        else
            Line1_Signed_ChData(i,k) = Line1_Exchange_ChData(i,k);
        end
    end  
end



%----------------------------------------------------------------------------
Line2_Header = fread(FileACQ, [1 8], 'uint8');
[Line2_Data ByteCnt] = fread(FileACQ, [USR_ChannelNum * USR_ByteNumPerSample USR_SampleNum ], 'uint8');
Line2_Data = Line2_Data';

for (i = 1 : USR_ChannelNum)
    Line2_ChData(:, i) = Line2_Data(:, 2*i-1) * 256 + Line2_Data(:, 2*i);  
end

for (i = 1 : 16)
    for (k = 1 : 4)
        Line2_Exchange_ChData(:, (i-1) * 4 + k) = Line2_ChData(:, (i-1) * 4 + (4+1-k));
    end
end

for (i = 1 :  USR_SampleNum)
    for (k = 1 : USR_ChannelNum)   
        if (Line2_Exchange_ChData(i,k) > 2^15)
            Line2_Signed_ChData(i,k) = (-1)* (2^16 - Line2_Exchange_ChData(i,k));
        else
            Line2_Signed_ChData(i,k) = Line2_Exchange_ChData(i,k);
        end
    end  
end


%----------------------------------------------------------------------------
Line3_Header = fread(FileACQ, [1 8], 'uint8');
[Line3_Data ByteCnt] = fread(FileACQ, [USR_ChannelNum * USR_ByteNumPerSample USR_SampleNum ], 'uint8');
Line3_Data = Line3_Data';

for (i = 1 : USR_ChannelNum)
    Line3_Data(:, i) = Line3_Data(:, 2*i-1) * 256 + Line3_Data(:, 2*i);  
end

for (i = 1 : 16)
    for (k = 1 : 4)
        Line3_Exchange_ChData(:, (i-1) * 4 + k) = Line3_Data(:, (i-1) * 4 + (4+1-k));
    end
end

for (i = 1 :  USR_SampleNum)
    for (k = 1 : USR_ChannelNum)   
        if (Line3_Exchange_ChData(i,k) > 2^15)
            Line3_Signed_ChData(i,k) = (-1)* (2^16 - Line3_Exchange_ChData(i,k));
        else
            Line3_Signed_ChData(i,k) = Line3_Exchange_ChData(i,k);
        end
    end  
end


%----------------------------------------------------------------------------
Line4_Header = fread(FileACQ, [1 8], 'uint8');
[Line4_Data ByteCnt] = fread(FileACQ, [USR_ChannelNum * USR_ByteNumPerSample USR_SampleNum ], 'uint8');
Line4_Data = Line4_Data';

for (i = 1 : USR_ChannelNum)
    Line4_Data(:, i) = Line4_Data(:, 2*i-1) * 256 + Line4_Data(:, 2*i);  
end

for (i = 1 : 16)
    for (k = 1 : 4)
        Line4_Exchange_ChData(:, (i-1) * 4 + k) = Line4_Data(:, (i-1) * 4 + (4+1-k));
    end
end

for (i = 1 :  USR_SampleNum)
    for (k = 1 : USR_ChannelNum)   
        if (Line4_Exchange_ChData(i,k) > 2^15)
            Line4_Signed_ChData(i,k) = (-1)* (2^16 - Line4_Exchange_ChData(i,k));
        else
            Line4_Signed_ChData(i,k) = Line4_Exchange_ChData(i,k);
        end
    end  
end
